from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from records.models import Record
from records.serializers import RecordSerializer, ReportSerializer


class RecordViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Record.objects.all()
    serializer_class = RecordSerializer
    permission_classes = [IsAuthenticated]
    # filterset_fields = ['type', 'project']  # noqa
    ordering = ['-create_time']  # 默认排序方式

    @action(methods=['get'], detail=True)
    def report(self, request, pk):
        record = self.get_object()
        serializer = ReportSerializer(record.report)
        return Response(serializer.data)
